package cloud.daodao.storm.user.helper;

import cloud.daodao.storm.common.file.api.FileApi;
import cloud.daodao.storm.common.file.model.FileData;
import cloud.daodao.storm.common.file.model.FileParam;
import cloud.daodao.storm.common.error.AppError;
import cloud.daodao.storm.common.error.AppException;
import cloud.daodao.storm.common.model.Request;
import cloud.daodao.storm.common.model.Response;
import cloud.daodao.storm.common.user.error.UserError;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class FileHandler {

    @Resource
    private FileApi fileApi;

    public FileData info(FileParam param) {
        Request<FileParam> request = new Request<>(param);
        Response<FileData> response;
        try {
            response = fileApi.info(request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new AppException(UserError.INVOKE_FILE_EXCEPTION, e);
        }
        if (!AppError.OK.code.equals(response.getCode())) {
            log.error("fileApi.info : {} : {}", request.toJson(), response.toJson());
            throw new AppException(UserError.INVOKE_FILE_EXCEPTION, response.toJson());
        }
        return response.getData();
    }

}
